//
//  SlideViewController.m
//  艾听歌
//
//  Created by dllo on 16/6/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SlideViewController.h"
#import "SlideTbaleView.h"
#import "SingerViewController.h"

@interface SlideViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) SlideTbaleView *rootTableView;
@property (nonatomic, strong) UIImageView *headerView;

@property (nonatomic, assign) BOOL canScroll;

@end

@implementation SlideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationController.navigationBarHidden = NO;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    
    //设置导航栏的电池栏的的背景
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
    [self.view addSubview:view1];
    view1.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:110 / 255.0 blue:142 / 255.0 alpha:0.8];
    
//    // 设置所有导航栏的颜色
//    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:248 / 255.0 green:110 / 255.0 blue:142 / 255.0 alpha:0.8]];
    
    
    SlideTbaleView *tableView = [[SlideTbaleView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    tableView.scrollsToTop = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.rootTableView = tableView;
    
    self.headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.rootTableView.frame), 200)];
//    [self.rootTableView addSubview:self.headerView];
    self.headerView.backgroundColor = [UIColor brownColor];
    self.rootTableView.tableHeaderView = self.headerView;
    [self.headerView sd_setImageWithURL:[NSURL URLWithString:self.supModel.picUrl] placeholderImage:[UIImage imageNamed:@"hold.png"]];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationAction:) name:wtLeaveTopNotificationName object:nil];
    self.canScroll = YES;// 默认是可以滚动的
    
 }


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithRed:248 / 255.0 green:110 / 255.0 blue:142 / 255.0 alpha:0.8]];
    self.navigationController.navigationBar.clipsToBounds = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:(UIBarMetricsDefault)];
    self.navigationController.navigationBar.clipsToBounds = NO;
}

- (void)notificationAction:(NSNotification *)notifition
{
    NSDictionary *usreInfo = notifition.userInfo;
    NSString *canScroll = usreInfo[@"canScroll"];
    if ([canScroll isEqualToString:@"1"]) {
        _canScroll = YES;
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        
        SingerViewController *singerVC = [[SingerViewController alloc] init];
        singerVC.singerId = self.supModel.singerId;
        [self addChildViewController:singerVC];
        
        [cell.contentView addSubview:singerVC.view];
    }
    
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = CGRectGetHeight(self.view.frame);
    return height;
}

// 主要逻辑:
// 当滑动一个Tableview时,两个Tableview同时会动,我们要做的是什么时候让大的动,什么时候让小的动
// 刚开始滑动两个一起动,当滑动到标题位置的时候,让大的保持在这个位置,发出通知让小的继续动
// 当小的往下滑动,滑动到偏移量为(0,0)点的时候,让小的保持在这个位置.并发出通知让大的开始动.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat tabOffsetY = [self.rootTableView rectForSection:0].origin.y;
    CGFloat offsetY = scrollView.contentOffset.y;
    
    if (offsetY >= tabOffsetY) {
        scrollView.contentOffset = CGPointMake(0, tabOffsetY);
        [[NSNotificationCenter defaultCenter] postNotificationName:wtGoTopNotificationName object:nil userInfo:@{@"canScroll":@"1"}];
        _canScroll = NO;
    }else
    {
        if (!_canScroll) {
            scrollView.contentOffset = CGPointMake(0, tabOffsetY);
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
